package qj.util.tree;

import qj.util.Cols;
import qj.util.funct.F1;
import qj.util.funct.Fs;

import java.util.List;

public class TreeNode<A> {
    private Object value;

    List childs;

    public TreeNode() {
    }

    public TreeNode(A value, String... childs) {
        this.value = value;
        if (childs != null && childs.length > 0) {
            this.childs = Cols.yield(childs, new F1<String, Object>() {
                public Object e(String obj) {
                    return new TreeNode(obj);
                }
            });
        }
    }


    public TreeNode getNode(List<Integer> path) {
        if (path.size() == 0) {
            return this;
        }
        TreeNode current = this;
        for (Integer integer : path) {
            current = (TreeNode) current.getChilds().get(integer);
        }
        return current;
    }

    public boolean isLeaf() {
        return childs == null;
    }

    public A getValue() {
        return (A) Fs.deepEval(value);
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public List getChilds() {
        return childs;
    }

    public void setChilds(List childs) {
        this.childs = childs;
    }
}
